package com.shengzai.rdd

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.immutable
import scala.util.Random

object Demo19SparkPi {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
    conf.setMaster("local[30]")
    conf.setAppName("join")

    val sc = new SparkContext(conf)

    val list: immutable.Seq[Int] = 0 until 10000000

    val RDD: RDD[Int] = sc.parallelize(list, 30)

    val pointRDD: RDD[(Double, Double)] = RDD.map(
      line => {
        val x: Double = Random.nextDouble() * 2 - 1
        val y: Double = Random.nextDouble() * 2 - 1
        (x, y)
      }
    )

    val circularPoint: RDD[(Double, Double)] = pointRDD.filter(kv => kv._1 * kv._1 + kv._2 * kv._2 < 1)

    val PI: Double =  circularPoint.count() / pointRDD.count().toDouble * 4.0
    println(PI)

  }

}
